<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车牌号" prop="plateId">
        <el-input
          v-model="queryParams.plateId"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="缴费状态" prop="plateState">
        <el-input
          v-model="queryParams.plateState"
          placeholder="请输入缴费状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="相关企业" prop="enterpriseId">
        <el-input
          v-model="queryParams.enterpriseId"
          placeholder="请输入相关企业"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['energy:tcCar:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['energy:tcCar:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['energy:tcCar:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['energy:tcCar:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tcCarList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="停车场" align="center" prop="parkName" />
      <el-table-column label="车牌号" align="center" prop="plateId" />
      <!--<el-table-column label="车牌颜色" align="center" prop="plateColor" />-->
      <!--<el-table-column label="车位号" align="center" prop="spaceIdentifier" />-->
      <!--<el-table-column label="Etc序号" align="center" prop="etcNum" />-->
      <el-table-column label="缴费类型" align="center" prop="plateType" />
      <el-table-column label="充值金额" align="center" prop="rechargeAmount" />
      <el-table-column label="开始时间" align="center" prop="beginDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车主姓名" align="center" prop="carownName" />
      <el-table-column label="车主电话" align="center" prop="carownPhone" />
      <el-table-column label="所属企业" align="center" prop="enterpriseId" />
      <el-table-column label="同步状态" align="center" prop="syncStateNew" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['energy:tcCar:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['energy:tcCar:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSync(scope.row)"
            v-hasPermi="['energy:tcCar:edit']"
          >同步</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改内部车辆对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="停车场名称" prop="parkName">
          <el-select clearable v-model="form.parkId" placeholder="请选择停车场">
            <el-option v-for="item in parkList" :key="item.id" :label="item.parkName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车牌号" prop="plateId">
          <el-input v-model="form.plateId" placeholder="请输入车牌号" />
        </el-form-item>
        <!--<el-form-item label="是否进入" prop="isinout">-->
          <!--<el-input v-model="form.isinout" placeholder="请输入是否进入" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="车牌颜色" prop="plateColor">-->
          <!--<el-input v-model="form.plateColor" placeholder="请输入车牌颜色" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="车位号" prop="spaceIdentifier">-->
          <!--<el-input v-model="form.spaceIdentifier" placeholder="请输入车位号" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="Etc序号" prop="etcNum">-->
          <!--<el-input v-model="form.etcNum" placeholder="请输入Etc序号" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="缴费类型" prop="plateType">-->
          <!--&lt;!&ndash;<el-select v-model="form.plateType" placeholder="请选择缴费类型">&ndash;&gt;-->
            <!--&lt;!&ndash;<el-option label="请选择字典生成" value="" />&ndash;&gt;-->
          <!--&lt;!&ndash;</el-select>&ndash;&gt;-->
        <!--</el-form-item>-->



        <!--<el-form-item label="缴费子类型" prop="plateSubtype">-->
          <!--<el-select v-model="form.plateSubtype" placeholder="请选择缴费子类型">-->
            <!--<el-option label="请选择字典生成" value="" />-->
          <!--</el-select>-->
        <!--</el-form-item>-->

        <el-form-item label="车主姓名" prop="carownName">
          <el-input v-model="form.carownName" placeholder="请输入车主姓名" />
        </el-form-item>
        <el-form-item label="车主电话" prop="carownPhone">
          <el-input v-model="form.carownPhone" placeholder="请输入车主电话号码" />
        </el-form-item>
        <el-form-item label="开始时间" prop="beginDate">
          <el-date-picker clearable size="small"
            v-model="form.beginDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endDate">
          <el-date-picker clearable size="small"
            v-model="form.endDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="充值金额" prop="rechargeAmount">
          <el-input v-model="form.rechargeAmount" placeholder="请输入充值金额" />
        </el-form-item>

<!--        <el-form-item label="区域名称" prop="areaName">-->
<!--          <el-input v-model="form.areaName" placeholder="请输入区域名称" />-->
<!--        </el-form-item>-->
        <!--<el-form-item label="时间戳" prop="timestamp">-->
          <!--<el-input v-model="form.timestamp" placeholder="请输入时间戳" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="签名" prop="signature">-->
          <!--<el-input v-model="form.signature" placeholder="请输入签名" />-->
        <!--</el-form-item>-->
        <el-form-item label="所属企业" prop="enterpriseId">
          <el-input v-model="form.enterpriseId" placeholder="请输入企业名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTcCar, getTcCar, delTcCar, addTcCar, updateTcCar, syncTcCar } from "@/api/smartParks/parking/tcCar";
import { listTcPark} from "@/api/smartParks/parking/tcPark";

export default {
  name: "TcCar",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 内部车辆表格数据
      tcCarList: [],
      parkIds:[],
      qyEnterpriseList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否删除字典
      delRecordOptions: [],
      parkList:[],
      // 允许重复进入字典
      allowInoutsOptions: [],
      leaseStates:[    {dictValue:'0',dictLabel:"未到期"},{dictValue:'1',dictLabel:"到期"}],
      carTypes:[    {dictValue:"0",dictLabel:"员工车辆"},{dictValue:"1",dictLabel:"其他车辆"}],
      plateStates:[      {dictValue:"0",dictLabel:"未缴费"},{dictValue:"1",dictLabel:"已缴费"}],
      plateTypes:[      {dictValue:"0",dictLabel:"临停车"},{dictValue:"1",dictLabel:"包月车"}],
      syncStates:[  {dictValue:"0",dictLabel:"未同步"},{dictValue:"1",dictLabel:"已同步"}],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        plateId: null,
        isinout: null,
        etcNum: null,
        plateType: '免费车',
        plateState: null,
        syncState:null,
        carownName: null,
        carownPhone: null,
        enterpriseId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parkId: [
          { required: true, message: "停车场不能为空", trigger: "blur" }
        ],
        plateId: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        carownName: [
          { required: true, message: "车主姓名不能为空", trigger: "blur" }
        ],
        carownPhone: [
          { required: true, message: "车主电话号码不能为空", trigger: "blur" }
        ],
        beginDate: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endDate: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    // this.getDicts("sys_yes_no").then(response => {
    //   this.delRecordOptions = response.data;
    // });
    // this.getDicts("sys_yes_no").then(response => {
    //   this.allowInoutsOptions = response.data;
    // });
    listTcPark().then((response) => {
      this.parkList = response.rows;
    })
  },
  methods: {
    /** 查询内部车辆列表 */
    getList() {
      this.loading = true;
      listTcCar(this.queryParams).then(response => {
          response.rows.forEach((item) => {
            this.leaseStates.forEach((itemType) => {
              if (itemType.dictValue === item.leaseState) {
                item.leaseStateNew = itemType.dictLabel;
              }
            });
            this.carTypes.forEach((itemState) => {
              if (itemState.dictValue === item.carType) {
                item.carTypeNew = itemState.dictLabel;
              }
            });
            this.plateStates.forEach((itemWork) => {
              if (itemWork.dictValue === item.plateState) {
                item.plateStateNew = itemWork.dictLabel;
              }
            });
            this.plateTypes.forEach((itemType) => {
              if (itemType.dictValue === item.plateType) {
                item.plateTypeNew = itemType.dictLabel;
              }
            });
            this.syncStates.forEach((itemWork) => {
              if (itemWork.dictValue === item.syncState) {
                item.syncStateNew = itemWork.dictLabel;
              }
            });
          });
        this.tcCarList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否删除字典翻译
    delRecordFormat(row, column) {
      return this.selectDictLabel(this.delRecordOptions, row.delRecord);
    },
    // 允许重复进入字典翻译
    allowInoutsFormat(row, column) {
      return this.selectDictLabel(this.allowInoutsOptions, row.allowInouts);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parkId: null,
        plateIdOld: null,
        plateId: null,
        isinout: null,
        plateColor: null,
        carType: null,
        carModel: null,
        spaceIdentifier: null,
        etcNum: null,
        plateType: null,
        plateState: null,
        plateSubtype: null,
        freeTime: null,
        fuzzyMatching: null,
        rechargeAmount: null,
        beginDate: null,
        endDate: null,
        carownName: null,
        carownSex: null,
        carownPhone: null,
        carownCardtype: null,
        carownCardnum: null,
        carownBirsday: null,
        carownAddress: null,
        chargScheme: null,
        delRecord: null,
        parkNum: null,
        allowInouts: null,
        areaName: null,
        timestamp: null,
        signature: null,
        enterpriseId: null,
        createBy: null,
        createTime: null,
        leaseState: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加内部车辆";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTcCar(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改内部车辆";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTcCar(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.plateType = '免费车'
            addTcCar(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除内部车辆编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTcCar(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    handleSync(row){
      const id = row.id || this.ids;
      let plateId = row.plateId;
     this.$confirm('是否确认同步车牌为'+ plateId +'的数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return syncTcCar(id);
      }).then(() => {
        this.getList();
        this.msgSuccess("同步成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('energy/tcCar/export', {
        ...this.queryParams
      }, `energy_tcCar.xlsx`)
    }
  }
};
</script>
